package com.circle.rest.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.common.page.PageInfoBT;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.sys.model.DictData;
import com.circle.rest.modular.sys.model.DictGroup;
import com.circle.rest.modular.sys.service.IDictDataService;
import com.circle.rest.modular.sys.service.IDictGroupService;
import com.circle.rest.modular.sys.wrapper.DictDataWrapper;
import com.circle.rest.modular.sys.wrapper.DictGroupWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典组信息 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-08
 */
@Api(value = "字典组信息", tags = {"sys : 字典组信息API"})
@RestController
@RequestMapping("/sys/dict")
public class DictController {

    @Autowired
    private IDictGroupService dictGroupService;

    @Autowired
    private IDictDataService dictDataService;

    /**
     * 查询字典组信息列表
     */
    @ApiOperation("查询字典组信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int"),
            @ApiImplicitParam(name = "condition", value = "关键字", dataType = "String")
    })
    @RequestMapping(value = "/groupList", method = RequestMethod.GET)
    @Log(title = "查询字典组信息列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object groupList(Integer index, Integer size, String condition) {
        Page<DictGroup> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<DictGroup> wrapper = new QueryWrapper<>();
        if (ToolUtil.isNotEmpty(condition)) {
            wrapper.like("group_name", condition).or().like("remark", condition);
        }
        IPage<Map<String, Object>> mapIPage = dictGroupService.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new DictGroupWrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 添加字典组信息
     */
    @ApiOperation("添加字典组信息")
    @RequestMapping(value = "/groupAdd", method = RequestMethod.POST)
    @Log(title = "添加字典组信息", businessType = BusinessType.INSERT)
    @Permission
    public Object groupAdd(DictGroup dictGroup) {
        if (ToolUtil.isEmpty(dictGroup.getGroupKey())) {
            throw new ServiceException("字典组KEY不能为空");
        }
        if (ToolUtil.isEmpty(dictGroup.getGroupName())) {
            throw new ServiceException("字典组名称不能为空");
        }
        QueryWrapper<DictGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("group_key", dictGroup.getGroupKey());
        List<DictGroup> list = dictGroupService.list(wrapper);
        if (list.size() > 0) {
            throw new ServiceException("字典组KEY已存在");
        }
        dictGroup.setUpdateTime(LocalDateTime.now());
        dictGroup.setCreateTime(LocalDateTime.now());
        boolean save = dictGroupService.save(dictGroup);
        return new SuccessResponseData(save);
    }

    /**
     * 修改字典组信息
     */
    @ApiOperation("修改字典组信息")
    @RequestMapping(value = "/groupEdit", method = RequestMethod.POST)
    @Log(title = "修改字典组信息", businessType = BusinessType.UPDATE)
    @Permission
    public Object groupEdit(DictGroup dictGroup) {
        if (ToolUtil.isEmpty(dictGroup.getId())) {
            throw new ServiceException("字典组编号不能为空");
        }
        if (ToolUtil.isEmpty(dictGroup.getGroupKey())) {
            throw new ServiceException("字典组KEY不能为空");
        }
        if (ToolUtil.isEmpty(dictGroup.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        QueryWrapper<DictGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("group_key", dictGroup.getGroupKey());
        List<DictGroup> list = dictGroupService.list(wrapper);
        if (list.size() > 0) {
            throw new ServiceException("字典组KEY已存在");
        }
        dictGroup.setUpdateTime(LocalDateTime.now());
        if (!dictGroupService.updateById(dictGroup)) {
            throw new ServiceException("更新失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除字典组信息
     */
    @ApiOperation("删除字典组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典组编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/groupDelete", method = RequestMethod.GET)
    @Log(title = "删除字典组信息", businessType = BusinessType.DELETE)
    @Permission
    public Object groupDelete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("字典组编号不能为空");
        }
        DictGroup dictGroupOld = dictGroupService.getById(id);
        if (ToolUtil.isEmpty(dictGroupOld)) {
            throw new ServiceException("字典组信息不存在，删除失败");
        }
        if (!dictGroupService.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询字典组信息
     */
    @ApiOperation("查询字典组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典组编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/groupDetail", method = RequestMethod.GET)
    @Log(title = "查询字典组信息", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object groupDetail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("字典组编号不能为空");
        }
        DictGroup dictGroup = dictGroupService.getById(id);
        if (ToolUtil.isEmpty(dictGroup)) {
            throw new ServiceException("字典组信息不存在");
        }
        return new SuccessResponseData(dictGroup);
    }

    /**
     * 查询字典数据信息列表
     */
    @ApiOperation("查询字典数据信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int"),
            @ApiImplicitParam(name = "condition", value = "关键字", dataType = "String"),
            @ApiImplicitParam(name = "groupKey", value = "字典组KEY", required = true, dataType = "String")
    })
    @RequestMapping(value = "/dataList", method = RequestMethod.GET)
    @Log(title = "查询字典数据信息列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object dataList(Integer index, Integer size, String condition, String groupKey) {
        if (ToolUtil.isEmpty(groupKey)) {
            throw new ServiceException("字典组KEY不能为空");
        }
        Page<DictData> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        wrapper.eq("group_key", groupKey);
        wrapper.orderByAsc("num");
        IPage<Map<String, Object>> mapIPage = dictDataService.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new DictDataWrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 添加字典数据信息
     */
    @ApiOperation("添加字典数据信息")
    @RequestMapping(value = "/dataAdd", method = RequestMethod.POST)
    @Log(title = "添加字典数据信息", businessType = BusinessType.INSERT)
    @Permission
    public Object dataAdd(DictData dictData) {
        if (ToolUtil.isOneEmpty(dictData.getDictLable(), dictData.getDictValue(), dictData.getGroupKey())) {
            throw new ServiceException("关键参数不能为空");
        }
        dictData.setUpdateTime(LocalDateTime.now());
        dictData.setCreateTime(LocalDateTime.now());
        boolean save = dictDataService.save(dictData);
        return new SuccessResponseData(save);
    }

    /**
     * 修改字典数据信息
     */
    @ApiOperation("修改字典数据信息")
    @RequestMapping(value = "/dataEdit", method = RequestMethod.POST)
    @Log(title = "修改字典数据信息", businessType = BusinessType.UPDATE)
    @Permission
    public Object dataEdit(DictData dictData) {
        if (ToolUtil.isEmpty(dictData.getId())) {
            throw new ServiceException("编号不能为空");
        }
        if (ToolUtil.isEmpty(dictData.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        dictData.setUpdateTime(LocalDateTime.now());
        if (!dictDataService.updateById(dictData)) {
            throw new ServiceException("更新失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除字典数据信息
     */
    @ApiOperation("删除字典数据信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典数据信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/dataDelete", method = RequestMethod.GET)
    @Log(title = "删除字典数据信息", businessType = BusinessType.DELETE)
    @Permission
    public Object dataDelete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("字典数据信息编号不能为空");
        }
        DictData dictDataOld = dictDataService.getById(id);
        if (ToolUtil.isEmpty(dictDataOld)) {
            throw new ServiceException("字典数据信息不存在，删除失败");
        }
        if (!dictDataService.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询字典数据信息
     */
    @ApiOperation("查询字典数据信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典数据信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/dataDetail", method = RequestMethod.GET)
    @Log(title = "查询字典数据信息", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object dataDetail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("字典数据信息编号不能为空");
        }
        DictData dictData = dictDataService.getById(id);
        if (ToolUtil.isEmpty(dictData)) {
            throw new ServiceException("字典数据信息不存在");
        }
        return new SuccessResponseData(dictData);
    }


}
